package no.uib.kwikktaim.core;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.JInternalFrame;
import javax.swing.event.*;

import java.awt.*;

/**
 * Sets of methods that is called for manipulating files
 */
public class FileHandling {
	/**
     * Open a media file.
     */
    public JMFrame openFile(String filename) {
		String mediaFile = filename;
		JMFrame frame = null;
		Player player = null;
		// URL for our media file
		URL url = null;
		try {
		    // Create an url from the file name and the url to the
		    // document containing this applet.
		    if ((url = new URL(mediaFile)) == null) {
			System.out.println("Can't build URL for " + mediaFile);
			return null;
		    }

		    // Create an instance of a player for this media
		    try {
			player = Manager.createPlayer(url);
		    } catch (NoPlayerException e) {
		    	System.out.println("Error: " + e);
		    }
		} catch (MalformedURLException e) {
			System.out.println("Error:" + e);
		} catch (IOException e) {
			System.out.println("Error:" + e);
		}
		if (player != null) {
		    frame = new JMFrame(player, filename);
		}
		return frame;

    }
}

/**
 * Internal frame for a player.
 *
 */
class JMFrame extends JInternalFrame implements ControllerListener {
    Player mplayer;
    Component visual = null;
    Component control = null;
    int videoWidth = 0;
    int videoHeight = 0;
    int controlHeight = 30;
    int insetWidth = 10;
    int insetHeight = 30;
    boolean firstTime = true;

    public JMFrame(Player player, String title) {
	super(title, true, true, true, true);
	getContentPane().setLayout( new BorderLayout() );
	setSize(320, 10);
	setLocation(50, 50);
	setVisible(true);
	mplayer = player;
	mplayer.addControllerListener((ControllerListener) this);
	mplayer.realize();
	addInternalFrameListener( new InternalFrameAdapter() {
	    public void internalFrameClosing(InternalFrameEvent ife) {
		mplayer.close();
	    }
	} );

}

    public void controllerUpdate(ControllerEvent ce) {
		if (ce instanceof RealizeCompleteEvent) {
		    mplayer.prefetch();
		} else if (ce instanceof PrefetchCompleteEvent) {
		    if (visual != null)
			return;

		    if ((visual = mplayer.getVisualComponent()) != null) {
			Dimension size = visual.getPreferredSize();
			videoWidth = size.width;
			videoHeight = size.height;
			getContentPane().add("Center", visual);
		    } else
			videoWidth = 320;
		    if ((control = mplayer.getControlPanelComponent()) != null) {
			controlHeight = control.getPreferredSize().height;
			getContentPane().add("South", control);
		    }
		    setSize(videoWidth + insetWidth,
			    videoHeight + controlHeight + insetHeight);
		    validate();
		    mplayer.start();
		} else if (ce instanceof EndOfMediaEvent) {
		    mplayer.setMediaTime(new Time(0));
		    mplayer.start();
		}
    }
}
